package com.shrio.controller;

import com.google.zxing.BarcodeFormat;
import com.shrio.QrCodeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * Created by luojie on 2018/4/8.
 */
@Controller
public class QrCodeController {

    @RequestMapping("encode")
    public String encode(HttpServletRequest request, ModelMap modelMap){
        String path = request.getContextPath();
        System.out.println("-------path-----"+path);
//        System.out.println("--------------"+request.getServletContext().getRealPath("/"));
//        System.out.println(request.getSession().getServletContext().getRealPath("/"));
//        System.out.println("---getContextPath----" + request.getContextPath());
        String content = "123456789";
        path = path + "qrCode/" + content +".png";
        String imgPath = content +".png";
        File file = new File(path);
        if(!file.exists()){
            QrCodeUtil.encode(content, file, BarcodeFormat.QR_CODE, 200, 200, null);
            System.out.println("---------encode success---------");
        }else {
            QrCodeUtil.decode(file);
            System.out.println("---------decode success---------");
        }
        modelMap.addAttribute("path", imgPath);
        return "qrCode";

    }
}
